#include "interfaceeditor.h"
#include "ui_interfaceeditor.h"
#include "ui_widgeteditor.h"
#include <QMessageBox>
#include <QInputDialog>
#include <QDebug>
#include <iostream>

using namespace Shiny;

InterfaceEditor::InterfaceEditor(QWidget *parent) : QWidget(parent), ui(new Ui::InterfaceEditor)
{
    _currentUi     = 0;
    _currentWidget = 0;

    ui->setupUi(this);
    ui->editor->setEnabled(false);

    connect(ui->addWidget,     SIGNAL(clicked()),                  this, SLOT(AddWidget()));
    connect(ui->delInterface,  SIGNAL(clicked()),                  this, SLOT(DelWidget()));
    connect(ui->loadUi,        SIGNAL(clicked()),                  this, SLOT(LoadUi()));
    connect(ui->listInterface, SIGNAL(currentRowChanged(int)),     this, SLOT(SelectWidget(int)));
    connect(ui->listInterface, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(SelectWidgetChildren()));
    connect(ui->parentWidget,  SIGNAL(clicked()),                  this, SLOT(SelectWidgetParent()));

    new Shiny::EventManager();
    connect(&_textureManager, SIGNAL(TextureAdded(QString)), ui->widgetEditor, SLOT(TextureAdded(QString)));
}

InterfaceEditor::~InterfaceEditor()
{
    delete Shiny::EventManager::Get();
    delete ui;
}

void InterfaceEditor::LoadUi()
{
    QString   texPath = _path + "/";
    QString   toLoad  = _path + "/" + ui->pathUi->text();

    if (_currentUi)
	delete _currentUi;
    _currentUi = DataTree::Factory::Yaml(toLoad.toStdString());
    if (_currentUi)
    {
	Shiny::Widgets::Ui* shinyUi;
	Data                currentUi(_currentUi);

	texPath += currentUi["textures"].Value().c_str();
	_textureManager.SetDirectory(_path + "/textures");
	_textureManager.Clear();
	_textureManager.Load(texPath);
	ui->interfacePainter->SetTextureManager(&_textureManager);
	ui->interfacePainter->LoadUi(currentUi);
	shinyUi = ui->interfacePainter->Ui();
	if (shinyUi)
	{
	    shinyUi->MouseReleased.Connect(*this, &InterfaceEditor::WidgetClicked);

	    WidgetClicked(shinyUi, sf::Event());
	    ui->editor->setEnabled(true);
	    _currentWidgetData = currentUi;
	}
    }
}

void InterfaceEditor::WidgetClicked(Shiny::Widgets::Container* widget, sf::Event)
{
    Shiny::Widgets::Container::Children::iterator it  = widget->ChildrenBegin();
    Shiny::Widgets::Container::Children::iterator end = widget->ChildrenEnd();

    _currentWidget     = widget;
    _currentList.clear();
    ui->listInterface->clear();
    for (; it != end ; ++it)
    {
	QPair<QString, Shiny::Widgets::Container*> pair;

	pair.first =  (*it)->GetName().c_str();
	pair.second = (*it);
	_currentList << pair;
	ui->listInterface->addItem(pair.first);
    }
    ui->widgetEditor->SetWidget(_currentWidgetData, _currentWidget);
    ui->delInterface->setEnabled(ui->listInterface->count() != 0);
}

void InterfaceEditor::SelectWidget(int row)
{
    Shiny::Widgets::Container* widget = GetCurrentWidget();

    if (widget)
	ui->widgetEditor->SetWidget(_currentWidgetData["widgets"][widget->GetName()], widget);
}

void InterfaceEditor::SelectWidgetChildren()
{
    Shiny::Widgets::Container* widget = GetCurrentWidget();

    if (widget)
    {
	_currentWidgetData = _currentWidgetData["widgets"][widget->GetName()];
	WidgetClicked(widget, sf::Event());
	ui->parentWidget->setEnabled(true);
    }
}

void InterfaceEditor::SelectWidgetParent()
{
    if (_currentWidget)
    {
	Shiny::Widgets::Container* widget = _currentWidget->Father();

	if (widget)
	{
	    _currentWidgetData = _currentWidget->Father()->Father();
	    WidgetClicked(widget, sf::Event());
	    if (widget->Father() == 0)
	      ui->parentWidget->setEnabled(false);
	}
    }
}

Shiny::Widgets::Container* InterfaceEditor::GetCurrentWidget()
{
    QListWidgetItem* item = ui->listInterface->currentItem();

    if (item)
    {
	QPair<QString, Shiny::Widgets::Container*> pair;

	ui->delInterface->setEnabled(true);
	foreach (pair, _currentList)
	{
	    if (pair.first == item->text())
		return (pair.second);
	}
    }
    else
	ui->delInterface->setEnabled(false);
    return (0);
}

void InterfaceEditor::DelWidget(void)
{
    Shiny::Widgets::Container* widget = GetCurrentWidget();
    Shiny::Widgets::Container* father = widget->Father();

    if (father)
    {
	int res = QMessageBox::warning(0, "Suppression de widget", QString("Etes-vous sur de vouloir supprimer ") + widget->GetName().c_str() + " ?", QMessageBox::Yes, QMessageBox::No);

	if (res == QMessageBox::Yes)
	{
	    father->DelWidget(widget);
	    delete widget;
	    delete ui->listInterface->currentItem();
	    if (ui->listInterface->count() == 0)
	    {
		if (father->Father())
		    WidgetClicked(father->Father(), sf::Event());
		else
		    ui->delInterface->setEnabled(false);
	    }
	}
    }
}

void InterfaceEditor::CreateWidgetFromEditor(void)
{
    Shiny::Widgets::Container* widget;
    sf::Vector2f               position;
    Shiny::Widgets::AlignType  halign;
    Shiny::Widgets::VAlignType valign;
    bool                       isVisible, isMovable;

    position.x = ui->widgetEditor->ui->posX->value();
    position.y = ui->widgetEditor->ui->posX->value();
    isVisible  = ui->widgetEditor->ui->isVisible->isChecked();
    isMovable  = ui->widgetEditor->ui->isMovable->isChecked();
    switch (ui->widgetEditor->ui->policyPosX->currentIndex())
    {
    case 0:
	halign = Shiny::Widgets::Left;
	break ;
    case 1:
	halign = Shiny::Widgets::Center;
	break ;
    case 2:
	halign = Shiny::Widgets::Right;
	break ;
    case 3:
	halign = Shiny::Widgets::None;
	break ;
    }
    switch (ui->widgetEditor->ui->policyPosY->currentIndex())
    {
    case 0:
	valign = Shiny::Widgets::Top;
	break ;
    case 1:
	valign = Shiny::Widgets::VCenter;
	break ;
    case 2:
	valign = Shiny::Widgets::Bot;
	break ;
    case 3:
	valign = Shiny::Widgets::VNone;
	break ;
    }
}

void InterfaceEditor::AddWidget(void)
{
    QString name = QInputDialog::getText(this, "Nouveau widget", "Nom du widget");

    if (name == "")
	return ;
    switch (ui->widgetList->currentIndex())
    {
    case 0: // Container
	/*Widgets::Container* container = new Widgets::Container();

	container->SetName(name.toStdString());*/
	break ;
    case 1: // TextWidget
	break ;
    }
}
